/**
 * login page js
 */
"use strict";
jQuery(window).load(function() {

	// Page Preloader
	jQuery('#preloader').delay(100).fadeOut(function(){
		jQuery('body').delay(100).css({'overflow':'visible'});
	});
});

define(function(require, exports) {

	//加载依赖
	require("jdialog");

	//绑定登陆事件
	$("#submit").on("click", function() {

		var username = $("#username").val().trim();
		var password = $("#password").val().trim();
		if ( username == '' ) return exports.message('error', '请输入用户名');
		if ( password == '' ) return exports.message('error', '请输入密码');

		if($('#scode').length > 0){
			var scode = $("#scode").val().trim();
			if ( scode == '' ) return exports.message('error', '请输入验证码');
		}

		$(this).button("loading");
		var __self = this;
		$.post("/admin/login/signin", {
			uname : username,
			pwd : password,
			scode : scode
		}, function(data) {

			if ( data.code == 0 ) {
				exports.message('ok', '登陆成功');
				location.replace(exports.uri);
			} else {
				exports.message('error', data.message);
                $("#scode").val('');
                $('.code img').attr('src','/utils/verifyCode/?s=0&run'+Math.random());
			}

			$(__self).button("reset");
            return false;

		}, "json");
        return false;
    });

	//绑定回车事件
	$(document).on("onsubmit", function(e) {
		if( e.keyCode==13 ) {
			$("#submit").trigger("click");
		}
	});

	exports.message = function(type, message) {

		JDialog.tip.work({
			type : type,
			container : '.login-box',
			top : 120,
			content : message,
			lock : false,
			timer : 3000
		});
        return false;
	}

});
